package com.mc.design.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class StudyProxy implements InvocationHandler {

	private Object target;

	/**
	 * 核心的获取代理对象的方法
	 * @param obj
	 * @return
	 */
	public Object getProxy(Object obj) {
		this.target = obj;
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
	}

	private void openBook() {
		System.out.println("打开书籍...");
	}

	private void closeBook() {
		System.out.println("关闭书籍");
	}

	//此方法代理接口。
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		openBook();
		Object result = method.invoke(target, args);
		closeBook();
		return result;
	}

}
